<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
     //封装双向链表
     function DoublyLinkedList(){
          //内部类,节点类
         function Node(data){
             this.data = data
             this.prev = null
             this.next = null
         }

         //属性
         this.head = null
         this.tail = null
         this.length = 0

         //常见操作:方法
         //1.append方法
         DoublyLinkedList.prototype.append = function (data) {
             //1.根据data创建节点
             var newNode = new Node(data)

             //2.判断添加的是否是第一个节点
             if(this.length == 0){
                  this.head = newNode
                  this.tail = newNode
             }else{
                 newNode.prev = this.tail
                 this.tail.next = newNode
                 this.tail = newNode
             }

             //3.length+1
             this.length += 1
         }

         //2.1 toString方法
         DoublyLinkedList.prototype.toString = function (){
             return this.backwardString()
         }

         //2.2.forwardString方法
         DoublyLinkedList.prototype.forwardString = function (){
              //1.定义变量
             var current = this.tail
             var resultString = ""

             //2.依次向前遍历,获取每一个节点
             while(current){
                 resultString += current.data + " "
                 current = current.prev
             }
             return resultString
         }

         //2.3.backwardString 方法
         DoublyLinkedList.prototype.backwardString = function () {
              //1.定义变量
             var current = this.head
             var resultString = ""

             //2.依次向后遍历
             while(current){
                 resultString += current.data + " "
                 current = current.next;
             }

             return resultString
         }
         //3.1 insert方法
         DoublyLinkedList.prototype.insert = function (position,data) {
                   //越界判断
                   if (position < 0 || position > this.length) return false;
                   //获取新的节点
                   var newNode =  new Node(data);

                   //插入操作
                   if(this.length == 0){
                       this.head = newNode;
                       this.tail = newNode;
                   }else{
                        if(position == 0){
                             this.head.prev = newNode;
                             newNode.next = this.head
                            this.head = newNode
                        }else if(position == this.length){
                             this.tail.next = newNode;
                             newNode.prev = this.tail;
                             this.tail = newNode;
                        }else{
                               var current = this.head ;
                               var index = 0;
                               while (index++ > position){
                                     current = current.next
                               }
                               current.prev.next = newNode;
                               newNode.prev = current.prev;
                               current.prev = newNode;
                               newNode.next = current;
                        }
                   }
                   this.length +=1 ;
         }
         //4.1 get方法
         DoublyLinkedList.prototype.get = function(position){
             //越界判断
              if(position < 0 || position >= this.length)  return false;

              //this.length / 2 > position
              //this.length / 2 < position

             //定义变量
             var current = this.head
             var index = 0

             while(index++ < position){
                 current = current.next
             }

             return current.data
         }
         //5.1 indexOf方法
         DoublyLinkedList.prototype.indexOf = function (data) {
             //定义变量
               var current =  this.head
               var index = 0;

               while(current){

                   if(current.data == data){
                       return index;
                   }

                   current = current.next
                   index +=1;
               }
               return  -1;
         }
         //6.1 update方法
         DoublyLinkedList.prototype.update = function(position,data){
             //越界判断
                 if( position < 0 || position > this.length ) return false;

                  //定义变量
             var current = this.head
             var index = 0;

             while(index++ < position){
                 current = current.next
             }
                current.data = data;
         }
         DoublyLinkedList.prototype.removeAt = function (position){
                if(position < 0 || position >= this.length ) return false;

                var current = this.head;
                if(this.length == 1){
                    this.head = null
                    this.tail = null
                }else{
                    if(position == 0){
                        this.head.next.prev = null
                         this.head = this.head.next;
                    }else if(position == this.length-1 ){
                        current = this.tail
                        this.tail.prev.next = null;
                        this.tail=this.tail.prev;
                    }else{
                        var index = 0;
                        while (index++ < position){
                             current= current.next
                        }
                         current.next.prev =current.prev;
                        current.prev.next = current.next;
                    }
                }
                  return current.data;
         }
     }
   var List = new DoublyLinkedList();
     List.append('abc')
     List.append('bbc')
     List.insert(2,'cbc')
     List.insert(0,'nbc')
    // alert(List.toString())
    // alert(List.forwardString())
    // alert(List.indexOf('nbc'))
    alert(List.removeAt(1))
    alert(List.toString())
     alert(List.forwardString())
</script>
</body>
</html>